/*
 *
 *  Car Manager
 *  Copyright 2002-2007 Tommi Laukkanen
 *  http://www.substanceofcode.com
 *
 *  ExportForm.java
 *
 *  Created on December 28th 2004, 22:14
 */
package com.substanceofcode.carmanager.view;

import com.substanceofcode.carmanager.controller.CarManagerMIDlet;
import javax.microedition.lcdui.*;

/**
 * ExportForm is used when user wants to export data to file.
 * @author  Tommi Laukkanen
 */
public class ExportForm extends Form implements CommandListener {
    private CarManagerMIDlet m_midlet;
    private Command exportCommand = new Command("Export", Command.SCREEN, 1);
    private Command cancelCommand = new Command("Cancel", Command.SCREEN, 2);
    private TextField exportPath;
    private StringItem statusLabel;
    
    /** Creates a new instance of ExportForm */
    public ExportForm(CarManagerMIDlet midlet) 
    {
        super("Export");
        m_midlet = midlet;
        
        exportPath = new TextField(
                "Path (including filename)",
                "E:/CarManager.csv",
                64,
                TextField.ANY);
        this.append(exportPath);
        
        statusLabel = new StringItem("Status:", "");
        this.append( statusLabel );
        
        addCommand(exportCommand);
        addCommand(cancelCommand);
        setCommandListener(this);
    }
    
    /** Handles the Send and Cancel commands.
     *  Send command sends the list to server and server's PHP script
     *  generates the email and sends it to recipient.
     */
    public void commandAction(Command c, Displayable d)
    {
        if(c==exportCommand)
        {
            // TODO: Export
            String path = exportPath.getString();
            boolean success = m_midlet.exportFillUps(path);
            if( success==true ) {
                statusLabel.setText( "" );
                m_midlet.showMainCanvas();
            } else {
                statusLabel.setText( 
                        "Unable to export data. Try another location." );
            }
        }
        else if(c==cancelCommand)
        {
            m_midlet.showMainCanvas();
        }
    }

    
}
